<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
      <div class="toggle-button" @click="toggleMenu">|||</div>
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="activeHandle('/' + subItem.path)">
          <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
        </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      isCollapse: false,
      // 当前激活的地址储存变量
      activePath: '',
      // 创建一个字体图标id对象
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created () {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 展开折叠分类
    toggleMenu () {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前激活的分类
    activeHandle (active) {
      // console.log(active)
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    },
    // 点击列表二级菜单显示高亮

    // 获取菜单数据
    async getMenus () {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      // 判断状态码
      // 如果状态不是200 返回提示信息
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 成功之后 添加数据给data中的属性
      this.menulist = res.data
    },
    // 退出方法
    logout () {
      // 清除本地的token
      window.sessionStorage.clear()
      // 使用编程式导航到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    font-size: 18px;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  .toggle-button {
    background-color: #4a5064;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
  }
  background-color: #333744;
  .el-menu {
    border: none
  }
  .iconfont {
    margin-right: 10px;
  }
}
.el-main {
  background-color: #eaedf1;
}
}
</style>
